﻿/* System.Compute: a Computation Framework for .NET/Mono
 * Copyright (C) 2011 Ralf "raboni" Boeckhorst
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 */

using System.Runtime.InteropServices;

namespace System.Compute.OpenCL
{
    public sealed class DeviceSampler : ICloneable, IDisposable
    {
        [DllImport("opencl.dll", ExactSpelling = true)]
        static extern IntPtr clCreateSampler(IntPtr context, bool normalized_coords, AddressingMode addressing_mode, FilterMode filter_mode, out ErrorCode errcode_ret);
        
        [DllImport("opencl.dll", ExactSpelling = true)]
        static extern ErrorCode clRetainSampler(IntPtr sampler);

        [DllImport("opencl.dll", ExactSpelling = true)]
        static extern ErrorCode clReleaseSampler(IntPtr sampler);

        [DllImport("opencl.dll", ExactSpelling = true)]
        static extern ErrorCode clGetSamplerInfo(IntPtr sampler, SamplerInfo param_name, SizeT param_value_size, ref uint param_value, SizeT param_value_size_ret);

        [DllImport("opencl.dll", ExactSpelling = true)]
        static extern ErrorCode clGetSamplerInfo(IntPtr sampler, SamplerInfo param_name, SizeT param_value_size, ref IntPtr param_value, SizeT param_value_size_ret);

        [DllImport("opencl.dll", ExactSpelling = true)]
        static extern ErrorCode clGetSamplerInfo(IntPtr sampler, SamplerInfo param_name, SizeT param_value_size, ref AddressingMode param_value, SizeT param_value_size_ret);

        [DllImport("opencl.dll", ExactSpelling = true)]
        static extern ErrorCode clGetSamplerInfo(IntPtr sampler, SamplerInfo param_name, SizeT param_value_size, ref FilterMode param_value, SizeT param_value_size_ret);

        public uint ReferenceCount
        {
            get
            {
                uint ret = 0;
                ErrorCode res = clGetSamplerInfo(Handle, SamplerInfo.ReferenceCount, sizeof(uint), ref ret, 0);
                if (res != ErrorCode.Success)
                    throw new OpenCLException(res);
                return ret;
            }
        }

        public Context Context
        {
            get
            {
                IntPtr ret = (IntPtr)0;
                ErrorCode res = clGetSamplerInfo(Handle, SamplerInfo.Context, IntPtr.Size, ref ret, 0);
                if (res != ErrorCode.Success)
                    throw new OpenCLException(res);
                return new Context(ret);
            }
        }

        public AddressingMode AddressingMode
        {
            get
            {
                AddressingMode ret = 0;
                ErrorCode res = clGetSamplerInfo(Handle, SamplerInfo.AddressingMode, sizeof(AddressingMode), ref ret, 0);
                if (res != ErrorCode.Success)
                    throw new OpenCLException(res);
                return ret;
            }
        }

        public FilterMode FilterMode
        {
            get
            {
                FilterMode ret = 0;
                ErrorCode res = clGetSamplerInfo(Handle, SamplerInfo.FilterMode, sizeof(FilterMode), ref ret, 0);
                if (res != ErrorCode.Success)
                    throw new OpenCLException(res);
                return ret;
            }
        }

        public bool NormalizedCoords
        {
            get
            {
                uint ret = 0;
                ErrorCode res = clGetSamplerInfo(Handle, SamplerInfo.NormalizedCoords, sizeof(uint), ref ret, 0);
                if (res != ErrorCode.Success)
                    throw new OpenCLException(res);
                return ret > 0;
            }
        }

        internal IntPtr Handle;

        internal DeviceSampler(IntPtr handle)
        {
            Handle = handle;
        }

        public DeviceSampler(Context context, bool normalized_coords, AddressingMode addressing_mode, FilterMode filter_mode)
        {
            ErrorCode res;
            Handle = clCreateSampler(context.Handle, normalized_coords, addressing_mode, filter_mode, out res);
            if (res != ErrorCode.Success)
                throw new OpenCLException(res);
        }
        
        public object Clone()
        {
            ErrorCode res = clRetainSampler(Handle);
            if (res != ErrorCode.Success)
                throw new OpenCLException(res);
            return new DeviceBuffer(Handle);
        }

        public void Dispose()
        {
            ErrorCode res = clReleaseSampler(Handle);
            if (res != ErrorCode.Success)
                throw new OpenCLException(res);
            Handle = IntPtr.Zero;
        }
    }
}
